package com.trip_memories;

import java.util.UUID;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioGroup;
import blogic.AccesBussineLogic;
import blogic.BusinessLogic;
import blogic.Operations;
import dbmodels.Photo;
import dbtypes.PermisionPhoto;

public class EditPhoto extends Activity {

	private Photo photo;
	private BusinessLogic bl;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_edit_photo);

		bl = AccesBussineLogic.getBussinesLogic(this);

		refresh();
		populate();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.edit_photo, menu);
		return true;
	}

	private void refresh() {
		UUID photoid = UUID.fromString(getIntent().getStringExtra("photoId"));
		photo = bl.getPhoto(photoid);

	}

	private void populate() {
		((ImageView)findViewById(R.id.photo_edit)).setImageBitmap(Operations.convertFromBytesInBitmap(this, photo.getBytes(), 0));
		((EditText)findViewById(R.id.namePhotoEdit)).setText(photo.getName());
	}
	
	public boolean onSaveEdit(View v) {
		Photo p = new Photo();

		p.setBytes(photo.getBytes());
		p.setId(photo.getId());
		p.setAlbumid(photo.getAlbumid());

		String name = ((EditText) findViewById(R.id.namePhotoSave)).getText()
				.toString();

		p.setName(name);

		int radioid = ((RadioGroup) findViewById(R.id.permisionPhoto))
				.getCheckedRadioButtonId();
		p.setPermision(PermisionPhoto.Nothing);
		switch (radioid) {
		case R.id.nothing_photo: {
			p.setPermision(PermisionPhoto.Nothing);
			break;
		}
		case R.id.all_photo: {
			p.setPermision(PermisionPhoto.All);
			break;
		}
		}

		if (p.isValid()) {
			finish();
		} else {
			dialog();
		}

		return true;
	}

	public void dialog(){
		Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(R.string.photo_wrong);
		builder.setPositiveButton(android.R.string.cancel, null);
		AlertDialog dialog = builder.create();
		dialog.show();
	}
	
	public boolean onCancelEdit(View v) {
		finish();
		return true;
	}

}
